import { defineStore } from 'pinia'
import {ref} from 'vue'
export const useCounterStore = defineStore('cart', () => {
  let carts=ref([])
  function addCart(cartdata,goodsid){
    let username=localStorage.getItem('username')
    let usercart=carts.value.filter(item=>item.username==username)
    let pdCart=usercart.find(item=>item.goods.goods_id==goodsid)
    if(pdCart){
        pdCart.number++
    }
    else{
        carts.value.push({
            username:username,
            number:1,
            id:Math.floor(Math.random()*100000),
            goods:cartdata,
            checked:false
        })
    }
  }
  function delCart(id){
    carts.value=carts.value.filter(item=>item.id!=id)
  }

  return { carts,addCart,delCart }
},
{
    persist: true,
  },
)